package com.example.myapplication.tack_29.fragment;

import android.os.Bundle;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.data.HomeBanner;
import com.example.myapplication.tack_29.adapter.ArrangeAdapter;
import com.example.myapplication.tack_29.adapter.TitleAdapter;
import com.example.myapplication.tack_29.data.PostData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class JobFragment extends Fragment {
    private View view;
    private ViewPager banner;
    private SearchView search;
    private TextView button;
    private List<HomeBanner.RowsDTO> bannerList;
    private Handler handler;
    private TabLayout popular;
    private List<PostData.RowsDTO> listPost;
    private RecyclerView arrange;
    private RecyclerView Title;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_29_fragment_jop, container, false);
        init();
        getBanner();
        getProfession();
        arrange("快递员");
        initSearch();
        initTitle();
        return view;
    }

    private void initTitle() {
        String url = "prod-api/api/job/post/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, PostData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                PostData postData = (PostData)obj;
                if(postData.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Title.setLayoutManager(new GridLayoutManager(JobFragment.this.getContext(),3));
                            Title.setAdapter(new TitleAdapter(postData.getRows(), JobFragment.this.getContext(), new TitleAdapter.Transferring() {
                                @Override
                                public void data(String name) {
                                    arrange(name);
                                }
                            }));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initSearch() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                submit(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                change(newText);
                return true;
            }
        });
    }

    private void change(String newText) {
        if(!"".equals(newText) && !TextUtils.isEmpty(newText)){
            arrange(newText);
        }else{
            arrange("快递员");
        }
    }

    private void submit(String query) {
        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            arrange(query);
        }else{
            arrange("快递员");
        }
    }

    private void getProfession() {
        String url = "prod-api/api/job/post/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, PostData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                PostData postData = (PostData)obj;
                if(postData.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            listPost  = postData.getRows();
                            for (int i = 0; i < postData.getRows().size()-1; i++) {
                                for (int j = 0; j < postData.getRows().size()-1-i; j++) {
                                    if(postData.getRows().get(i).getProfessionId() > postData.getRows().get(j).getProfessionId()){
                                        PostData.RowsDTO temp = listPost.get(i);
                                        listPost.set(i, listPost.get(j));
                                        listPost.set(j, temp);
                                    }
                                }
                            }
                            for (int i = 0; i < 3; i++) {
                                TabLayout.Tab tab = popular.newTab();
                                tab.setText(listPost.get(i).getName());
                                popular.addTab(tab);
                            }

                            CilakPopular();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void CilakPopular() {
        popular.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String name = listPost.get(position).getName();
                arrange(name);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void arrange(String name) {
        String url = "prod-api/api/job/post/list?name="+name;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, PostData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                PostData postData = (PostData)obj;
                if(postData.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            arrange.setLayoutManager(new LinearLayoutManager(JobFragment.this.getContext()));
                            arrange.setAdapter(new ArrangeAdapter(postData.getRows(),JobFragment.this.getContext()));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init(){
        handler = new Handler();
        banner = view.findViewById(R.id.banner);
        search = view.findViewById(R.id.search);
        button = view.findViewById(R.id.button);
        popular = view.findViewById(R.id.popular);
        arrange = view.findViewById(R.id.arrange);
        Title = view.findViewById(R.id.Title);
    }
    private void getBanner() {
        String url = "prod-api/api/rotation/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeBanner.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HomeBanner homeBanner = (HomeBanner)obj;
                bannerList = homeBanner.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (HomeBanner.RowsDTO rowsDTO : bannerList) {
                            list.add(rowsDTO.getAdvImg());
                        }
                        BannerTool bannerTool = new BannerTool(JobFragment.this.getContext(),list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

}